/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Vue;

import Controleur.NouvelIncidentControleur;
import Modele.TypeIncident;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author 20cent
 */
public class NouvelIncident extends javax.swing.JFrame {

    private NouvelIncidentControleur controleur;

    /**
     * Creates new form NouvelIncident
     */
    public NouvelIncident() {
        initComponents();
        controleur = new NouvelIncidentControleur(this);
        controleur.recupererTypesIncidents();
    }
    
   

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        label_titre = new javax.swing.JLabel();
        label_no_serie = new javax.swing.JLabel();
        label_type = new javax.swing.JLabel();
        label_commentaire = new javax.swing.JLabel();
        tf_no_serie = new javax.swing.JTextField();
        cb_types = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        ta_com = new javax.swing.JTextArea();
        btn_valider = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBounds(new java.awt.Rectangle(0, 0, 0, 0));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);

        label_titre.setFont(new java.awt.Font("Trebuchet MS", 1, 24)); // NOI18N
        label_titre.setForeground(new java.awt.Color(255, 0, 51));
        label_titre.setIcon(new javax.swing.ImageIcon("C:\\Users\\20cent\\Desktop\\warning.png")); // NOI18N
        label_titre.setText("Signaler un Problème");

        label_no_serie.setText("Numéro de série du vélo");
        label_no_serie.setToolTipText("Vous pourrez trouver ce numéro sur le guidon du vélo.");

        label_type.setText("Type de problème");

        label_commentaire.setText("Commentaire");

        tf_no_serie.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tf_no_serieActionPerformed(evt);
            }
        });
        tf_no_serie.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tf_no_serieFocusLost(evt);
            }
        });

        cb_types.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cb_types.setEnabled(false);

        ta_com.setColumns(20);
        ta_com.setRows(5);
        jScrollPane1.setViewportView(ta_com);

        btn_valider.setText("Valider");
        btn_valider.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_validerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(310, 310, 310)
                        .addComponent(btn_valider))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(77, 77, 77)
                        .addComponent(label_titre))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(39, 39, 39)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jScrollPane1)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(label_no_serie, javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(label_commentaire, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 86, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(label_type, javax.swing.GroupLayout.Alignment.LEADING))
                                .addGap(32, 32, 32)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(tf_no_serie)
                                    .addComponent(cb_types, 0, 216, Short.MAX_VALUE))))))
                .addGap(79, 79, 79))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(label_titre)
                .addGap(56, 56, 56)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label_no_serie, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(tf_no_serie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(label_type, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cb_types, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(label_commentaire, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(66, 66, 66)
                .addComponent(btn_valider)
                .addGap(42, 42, 42))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tf_no_serieActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_no_serieActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_no_serieActionPerformed

    private void tf_no_serieFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tf_no_serieFocusLost
        // TODO add your handling code here:
    }//GEN-LAST:event_tf_no_serieFocusLost

    private void btn_validerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_validerActionPerformed
        int typeinc_id = ((TypeIncident) cb_types.getSelectedItem()).getMId();
        try {
            btn_valider.setEnabled(false);
            controleur.valideFormulaire(tf_no_serie.getText(), typeinc_id, ta_com.getText());
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this,
                    "Un nombre est attendu dans le champ \"numéro de série\".",
                    "Erreur de champ",
                    JOptionPane.WARNING_MESSAGE);
            
            activerBouton();
        }
    }//GEN-LAST:event_btn_validerActionPerformed

    public void setList(ArrayList<TypeIncident> liste) {
        cb_types.removeAllItems();
        for (TypeIncident type : liste) {
            cb_types.addItem(type);
        }
        cb_types.setEnabled(true);
    }
    
    public void reset(){
        tf_no_serie.setText("");
        cb_types.setSelectedIndex(0);
        ta_com.setText("");
    }
    
    public void activerBouton(){
        btn_valider.setEnabled(true);
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_valider;
    private javax.swing.JComboBox cb_types;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel label_commentaire;
    private javax.swing.JLabel label_no_serie;
    private javax.swing.JLabel label_titre;
    private javax.swing.JLabel label_type;
    private javax.swing.JTextArea ta_com;
    private javax.swing.JTextField tf_no_serie;
    // End of variables declaration//GEN-END:variables
}
